/*********************************************************************
This is a library for our Monochrome OLEDs based on SSD1325 drivers

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98

These displays use SPI to communicate, 4 or 5 pins are required to
interface

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/

#include <stdlib.h>
#include <SPI.h>
#include "gfx.h"
#include "glcdfont.c"
#include "ssd1325.h"


// Chip select of first shift register
#define SHIFT_REGISTER_CHIP_SELECT_1 ( 4 )

// Chip select of second shift register
#define SHIFT_REGISTER_CHIP_SELECT_2 ( 5 )

// a 5x7 font table
extern const uint8_t PROGMEM font[];

// buffer at startup contains logo
static uint8_t buffer[SSD1325_LCDHEIGHT * SSD1325_LCDWIDTH / 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x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 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x1f,
0x1f, 0x1f, 0x0f, 0x0f, 0x0f, 0x07, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};


void SSD1325::begin(void)
{
    SPI.begin();
    SPI.setDataMode(SPI_MODE0);
    SPI.setClockDivider(SPI_CLOCK_DIV2);

    pinMode(SHIFT_REGISTER_CHIP_SELECT_1, OUTPUT);
    pinMode(SHIFT_REGISTER_CHIP_SELECT_2, OUTPUT);
    pinMode(cs, OUTPUT);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_2, HIGH);
    digitalWrite(cs, HIGH);

    setRes(1);
    delay(1);
    setRes(0);
    delay(10);
    setRes(1);

    delay(500);

    startSendCommand();
    SPI.transfer(SSD1325_DISPLAYOFF); /* display off */
    SPI.transfer(SSD1325_SETCLOCK); /* set osc division */
    SPI.transfer(0xF1); /* 145 */
    SPI.transfer(SSD1325_SETMULTIPLEX ); /* multiplex ratio */
    SPI.transfer(0x3f); /* duty = 1/64 */
    SPI.transfer( SSD1325_SETOFFSET); /* set display offset --- */
    SPI.transfer(0x4C); /* 76 */
    SPI.transfer(SSD1325_SETSTARTLINE); /*set start line */
    SPI.transfer(0x00); /* ------ */
    SPI.transfer(SSD1325_MASTERCONFIG); /*Set Master Config DC/DC Converter*/
    SPI.transfer(0x02);
    SPI.transfer(SSD1325_SETREMAP); /* set segment remap------ */
    SPI.transfer(0x56);
    SPI.transfer(SSD1325_SETCURRENT + 0x1); /* Set Current Range */
    SPI.transfer(SSD1325_SETGRAYTABLE);
    SPI.transfer(0x01);
    SPI.transfer(0x11);
    SPI.transfer(0x22);
    SPI.transfer(0x32);
    SPI.transfer(0x43);
    SPI.transfer(0x54);
    SPI.transfer(0x65);
    SPI.transfer(0x76);
    SPI.transfer(SSD1325_SETCONTRAST); /* set contrast current */
    SPI.transfer(0x7F);  // max!
    SPI.transfer(SSD1325_SETROWPERIOD);
    SPI.transfer(0x51);
    SPI.transfer(SSD1325_SETPHASELEN);
    SPI.transfer(0x55);
    SPI.transfer(SSD1325_SETPRECHARGECOMP);
    SPI.transfer(0x02);
    SPI.transfer(SSD1325_SETPRECHARGECOMPENABLE);
    SPI.transfer(0x28);
    SPI.transfer(SSD1325_SETVCOMLEVEL); // Set High Voltage Level of COM Pin
    SPI.transfer(0x1C); //?
    SPI.transfer(SSD1325_SETVSL); // set Low Voltage Level of SEG Pin
    SPI.transfer(0x0D|0x02);
    SPI.transfer(SSD1325_NORMALDISPLAY); /* set display mode */
    SPI.transfer(0x15); /* set column address */
    SPI.transfer(0x00); /* set column start address */
    SPI.transfer(0x3f); /* set column end address */
    SPI.transfer(0x75); /* set row address */
    SPI.transfer(0x00); /* set row start address */
    SPI.transfer(0x3f); /* set row end address */
    SPI.transfer(SSD1325_DISPLAYON); /* display ON */
    stopSendCommand();
}


void SSD1325::sendBuffer(void)
{
    startSendData();

    for (uint16_t x=0; x<128; x+=2)
    {
        for (uint16_t y=0; y<64; y+=8)
        { // we write 8 pixels at once
            uint8_t left8 = buffer[y*16+x];
            uint8_t right8 = buffer[y*16+x+1];

            for (uint8_t p=0; p<8; p++)
            {
                uint8_t d = 0;

                if (left8 & (1 << p))
                {
                    d |= 0xF0;
                };

                if (right8 & (1 << p))
                {
                    d |= 0x0F;
                }

                SPI.transfer(d);
            }
        }
    }

    stopSendData();
}


void SSD1325::eraseBuffer(void)
{
    memset(buffer, 0, sizeof(buffer));
}


void SSD1325::drawPixel(int16_t x, int16_t y, uint16_t color)
{
    if ((x >= width()) || (y >= height()) || (x < 0) || (y < 0))
    {
        return;
    }

    // check rotation, move pixel around if necessary
    switch (getRotation())
    {
        case 1:
            gfx_swap(x, y);
            x = WIDTH - x - 1;
            break;
        case 2:
            x = WIDTH - x - 1;
            y = HEIGHT - y - 1;
            break;
        case 3:
            gfx_swap(x, y);
            y = HEIGHT - y - 1;
            break;
    }

    // x is which column
    if (color == WHITE)
    {
        buffer[x+ (y/8)*SSD1325_LCDWIDTH] |= _BV((y%8));
    }
    else
    {
        buffer[x+ (y/8)*SSD1325_LCDWIDTH] &= ~_BV((y%8));
    }
}


bool SSD1325::readButton()
{
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_2, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_2, LOW);
    delay(1);
    digitalWrite(SCK, LOW);
    delay(1);
    uint8_t data = SPI.transfer(0);
    delay(1);
    digitalWrite(SCK, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_2, HIGH);
    delay(1);

    if (data & 0x01)
    {
        return false;
    }
    else
    {
        return true;
    }
}


void SSD1325::enableRedLed()
{
    shift_register_data = ((0x01 << 4) & 0x70) | (shift_register_data & ~0x70);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}


void SSD1325::enableYellowLed()
{
    shift_register_data = ((0x02 << 4) & 0x70) | (shift_register_data & ~0x70);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}


void SSD1325::enableGreenLed()
{
    shift_register_data = ((0x04 << 4) & 0x70) | (shift_register_data & ~0x70);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}

void SSD1325::disableLeds()
{

    shift_register_data =  (shift_register_data & ~0x70);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}


void SSD1325::setDC(uint8_t value){
    shift_register_data = ((value << 7) & 0x80) | (shift_register_data & ~0x80);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}


void SSD1325::setRes(uint8_t value){
    shift_register_data = ((value << 3) & 0x08) | (shift_register_data & ~0x08);

    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
    delay(1);
    SPI.transfer(shift_register_data);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, HIGH);
    delay(1);
    digitalWrite(SHIFT_REGISTER_CHIP_SELECT_1, LOW);
}


void SSD1325::startSendCommand(void)
{
    setDC(0);
    digitalWrite(cs, LOW);
}


void SSD1325::stopSendCommand(void)
{
    digitalWrite(cs, HIGH);
    setDC(1);
}


void SSD1325::startSendData(void)
{
    setDC(1);
    digitalWrite(cs, LOW);
}


void SSD1325::stopSendData(void)
{
    digitalWrite(cs, HIGH);
    setDC(0);
}
